package com.archstoday.utils;

import android.content.Context;
import android.text.TextUtils;

import com.archstoday.common.SpfUtils;
import com.archstoday.entity.UserInfoEntity;
import com.base.utils.MySharedPreferencesMgr;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


/**
 * Created by brooks on 16/6/14.
 */
public class OAuthUtils {
    private static OAuthUtils instance = null;

    public static OAuthUtils getInstance() {
        if (instance == null) {
            synchronized (OAuthUtils.class) {
                instance = new OAuthUtils();
            }
        }

        return instance;
    }

    public static String username = "";
    String app_key = "";

    public static String getUsername() {
        return username;
    }

    public static void setUsername(String username) {
        OAuthUtils.username = username;
    }

    public static String token = "";

    public String getToken() {
        if (!TextUtils.isEmpty(token)) {
            return token;
        }

        token = MySharedPreferencesMgr.getString(SpfUtils.USER_TOKEN, "");
        if (!TextUtils.isEmpty(token)) {
            return token;
        }

        return token;
    }


    public void setToken(Context mContext, String t) {
        token = t;
    }

    public void cleanToken(Context mContext) {
        setToken(mContext, "");
//        PrefUtil.savePreferenceByItem(mContext, PrefUtil.arch_token_key, PrefUtil.arch_token_value, "");
    }

    public void setNick(String nick) {
        getUserInfo().setNickName(nick);
    }

    public void setSex(String sex) {
        getUserInfo().setSex(sex);
    }

    public void setAge(int age) {
        getUserInfo().setAge(age);
    }

    //用户信息
    public static UserInfoEntity.DataBean dataInfo = null;

    public UserInfoEntity.DataBean getUserInfo() {
        if (dataInfo != null) {
            return dataInfo;
        }

        String json = MySharedPreferencesMgr.getString(SpfUtils.USER_INFO, "");
        if (!TextUtils.isEmpty(json)) {
            Gson gson = new Gson();
            dataInfo = gson.fromJson(json, new TypeToken<UserInfoEntity.DataBean>() {
            }.getType());
            return dataInfo;
        }


        return dataInfo;
    }

    public void setUserInfo(UserInfoEntity.DataBean info) {
        this.dataInfo = info;
        MySharedPreferencesMgr.setString(SpfUtils.USER_INFO, new Gson().toJson(info));
    }

    public void cleanUserInfo() {
        this.dataInfo = null;
        MySharedPreferencesMgr.setString(SpfUtils.USER_INFO, "");
    }
}
